package com.zsc.roadsys.controller;

import lombok.extern.slf4j.Slf4j;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 封装控制器的基本功能,提供request、response、session等内置对象的获取方法
 */
@Slf4j
public class BaseController {
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest req, HttpServletResponse res) {
        this.request = req;
        this.response = res;
        this.session = req.getSession();
    }

    /**
     * 获取当前web项目的物理路径
     * @return
     */
    public String getBasePath() {
        return request.getServletContext().getRealPath("/");
    }

    /**
     * 获取当前登录的用户名
     * @return
     */
//    public String  getCurrentUsername(){
//        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
//                .getAuthentication()
//                .getPrincipal();
//        return userDetails.getUsername();
//    }
}
